{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Group the People Given the Group Size They Belong To"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: groupThePeople"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #用户分组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>有&nbsp;<code>n</code>&nbsp;个人被分成数量未知的组。每个人都被标记为一个从 <code>0</code> 到 <code>n - 1</code> 的<strong>唯一ID</strong>&nbsp;。</p>\n",
    "\n",
    "<p>给定一个整数数组 <code>groupSizes</code> ，其中<meta charset=\"UTF-8\" />&nbsp;<code>groupSizes[i]</code>&nbsp;是第 <code>i</code> 个人所在的组的大小。例如，如果&nbsp;<code>groupSizes[1] = 3</code>&nbsp;，则第 <code>1</code> 个人必须位于大小为 <code>3</code> 的组中。</p>\n",
    "\n",
    "<p>返回一个组列表，使每个人 <code>i</code> 都在一个大小为<meta charset=\"UTF-8\" /><em>&nbsp;<code>groupSizes[i]</code>&nbsp;</em>的组中。</p>\n",
    "\n",
    "<p>每个人应该&nbsp;<strong>恰好只&nbsp;</strong>出现在&nbsp;<strong>一个组&nbsp;</strong>中，并且每个人必须在一个组中。如果有多个答案，返回其中&nbsp;<strong>任何&nbsp;</strong>一个。可以&nbsp;<strong>保证&nbsp;</strong>给定输入&nbsp;<strong>至少有一个&nbsp;</strong>有效的解。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>groupSizes = [3,3,3,3,3,1,3]\n",
    "<strong>输出：</strong>[[5],[0,1,2],[3,4,6]]\n",
    "<strong>解释：\n",
    "</strong>第一组是 [5]，大小为 1，groupSizes[5] = 1。\n",
    "第二组是 [0,1,2]，大小为 3，groupSizes[0] = groupSizes[1] = groupSizes[2] = 3。\n",
    "第三组是 [3,4,6]，大小为 3，groupSizes[3] = groupSizes[4] = groupSizes[6] = 3。 \n",
    "其他可能的解决方案有 [[2,1,6],[5],[0,4,3]] 和 [[5],[0,6,2],[4,3,1]]。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>groupSizes = [2,1,3,3,3,2]\n",
    "<strong>输出：</strong>[[1],[0,5],[2,3,4]]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>groupSizes.length == n</code></li>\n",
    "\t<li><code>1 &lt;= n&nbsp;&lt;= 500</code></li>\n",
    "\t<li><code>1 &lt;=&nbsp;groupSizes[i] &lt;= n</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [group-the-people-given-the-group-size-they-belong-to](https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [group-the-people-given-the-group-size-they-belong-to](https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[3,3,3,3,3,1,3]', '[2,1,3,3,3,2]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, arr: List[int]) -> List[List[int]]:\n",
    "        g = defaultdict(list)\n",
    "        for i, v in enumerate(arr):\n",
    "            g[v].append(i)\n",
    "        res = []\n",
    "        for k in g:\n",
    "            i = 0\n",
    "            while i < len(g[k]):\n",
    "                res.append(g[k][i:i+k])\n",
    "                i += k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        # use dic\n",
    "        dic = {}\n",
    "        res = []\n",
    "\n",
    "        for i, v in enumerate(groupSizes):\n",
    "            if v not in dic:\n",
    "                dic[v] = []\n",
    "            dic[v].append(i)\n",
    "\n",
    "            if len(dic[v]) == v:\n",
    "                res.append(dic[v])\n",
    "                dic[v] = []\n",
    "        \n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "# https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/\n",
    "\n",
    "        \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        r = max(groupSizes)\n",
    "        s = [[] for _ in range(r+1)]\n",
    "        for i, v in enumerate(groupSizes):\n",
    "            s[v].append(i)\n",
    "            \n",
    "        ans = []\n",
    "        for i, v in enumerate(s):\n",
    "            j = 0\n",
    "            while j < len(v):\n",
    "                ans.append(v[j:j+i])\n",
    "                j += i\n",
    "                \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        n = len(groupSizes)\n",
    "        count = defaultdict(list)\n",
    "        results = []\n",
    "        for i in range(n):\n",
    "            number = groupSizes[i]\n",
    "            count[number].append(i)\n",
    "            if len(count[number]) == number:\n",
    "                results.append(count[number])\n",
    "                count[number] = []\n",
    "        \n",
    "        return results\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "\n",
    "        groups = {}\n",
    "        result = []\n",
    "\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            if size not in groups:\n",
    "                groups[size] = [i]\n",
    "            else:\n",
    "                groups[size].append(i)\n",
    "\n",
    "            if len(groups[size]) == size:\n",
    "                result.append(groups[size])\n",
    "                groups[size] = []\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        count_d = defaultdict(list)\n",
    "        total = []\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            count_d[str(v)].append(i)\n",
    "        for k,v in count_d.items():\n",
    "            res = []\n",
    "            for idx in v:\n",
    "                res.append(idx)\n",
    "                if len(res) == int(k):\n",
    "                    total.append(res)\n",
    "                    res = []\n",
    "                    \n",
    "            \n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        set_ = list(set(groupSizes))\n",
    "        list_,number = [],[]\n",
    "        list_1,list_2 = [],[]\n",
    "        for i in set_:\n",
    "            for j in range(len(groupSizes)):\n",
    "                if i == groupSizes[j]:\n",
    "                    number.append(j)\n",
    "            list_.append(number)\n",
    "            number = []\n",
    "        for i in range(len(set_)):\n",
    "            if len(list_[i]) % set_[i] == 0:\n",
    "                num_ = len(list_[i]) // set_[i]\n",
    "                for j in range(num_):\n",
    "                    list_1.append(list_[i][j*set_[i]:(j+1)*set_[i]])\n",
    "        return list_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        res = []\n",
    "        stack = defaultdict(set)\n",
    "\n",
    "        for i, g in enumerate(groupSizes):\n",
    "            if len(stack[g]) == g:\n",
    "                res.append(list(stack.pop(g)))\n",
    "            stack[g].add(i)\n",
    "        \n",
    "        for k in stack.keys():\n",
    "            res.append(list(stack[k]))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: list[int]) -> list[list[int]]:\n",
    "        d, res = defaultdict(list), []\n",
    "        for i, v in enumerate(groupSizes):\n",
    "            d[v].append(i)\n",
    "        for k, val in d.items():\n",
    "            res.extend([val[i:i+k] for i in range(0,len(val),k)])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        sizeMap = {}\n",
    "\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            if size in sizeMap.keys():\n",
    "                sizeMap[size].append(i)\n",
    "            else:\n",
    "                sizeMap[size] = [i]\n",
    "\n",
    "        res = []\n",
    "        for k, v in sizeMap.items():\n",
    "            while v:\n",
    "                temp = []\n",
    "                for _ in range(k):\n",
    "                    temp.append(v.pop())\n",
    "                res.append(temp)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: list[int]) -> list[list[int]]:\n",
    "        res, d = [], {}\n",
    "        for i, v in enumerate(groupSizes):\n",
    "            if i in d:\n",
    "                continue\n",
    "            if v == 1:\n",
    "                res.append([i])\n",
    "                continue\n",
    "            l = []\n",
    "            for n in range(v):\n",
    "                d.setdefault(i, 0)\n",
    "                l.append(i)\n",
    "                if (i != len(groupSizes)-1) and (n != v-1):\n",
    "                    i = groupSizes[i+1:].index(v)+i+1\n",
    "            res.append(l)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        n=len(groupSizes)\n",
    "        min0=min(groupSizes)\n",
    "        max0=max(groupSizes)\n",
    "        ans=[]\n",
    "        for i in range(min0,max0+1):\n",
    "            storelst=[]\n",
    "            c=0\n",
    "            for j in range(n):\n",
    "                if groupSizes[j]==i:\n",
    "                    c+=1\n",
    "                    storelst.append(j)\n",
    "                if c==i:\n",
    "                    ans.append(storelst)\n",
    "                    storelst=[]\n",
    "                    c=0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        groupSize_dict={}\n",
    "        ans=[]\n",
    "        for i,groupSize in enumerate(groupSizes):\n",
    "            if groupSize not in groupSize_dict:\n",
    "                groupSize_dict[groupSize]=[i]\n",
    "            else:\n",
    "                groupSize_dict[groupSize].append(i)\n",
    "            if len(groupSize_dict[groupSize])==groupSize:\n",
    "                ans.append(groupSize_dict[groupSize])\n",
    "                del groupSize_dict[groupSize]\n",
    "        return ans\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        \n",
    "        def listsplit(num,l):\n",
    "            r = []\n",
    "            times = int(len(l)/num)\n",
    "            if times == 1:\n",
    "                r.append(l)\n",
    "            else:\n",
    "                for i in range(times):\n",
    "                    m = []\n",
    "                    m = l[num*i:num*(i+1)]\n",
    "                    r.append(m)\n",
    "            return r\n",
    "        \n",
    "        assemble = set(groupSizes)\n",
    "        result = []\n",
    "        ListSprilt = []\n",
    "        for i in assemble:\n",
    "            middle = []\n",
    "            for j in range(len(groupSizes)):\n",
    "                if i == groupSizes[j]:\n",
    "                    middle.append(j)\n",
    "                else:\n",
    "                    continue\n",
    "            ListSprilt = listsplit(i,middle)\n",
    "            for i in ListSprilt:\n",
    "                result.append(i)\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        groups = defaultdict(list)\n",
    "        for i,size in enumerate(groupSizes):\n",
    "            groups[size].append(i)\n",
    "        ans = []\n",
    "        for size,people in groups.items():\n",
    "            ans.extend(people[i:i+size]for i in range(0,len(people),size))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        groups = defaultdict(list)\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            groups[size].append(i)\n",
    "        ans = []\n",
    "        for size, people in groups.items():\n",
    "            ans.extend(people[i: i + size] for i in range(0, len(people), size))\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        cnt = defaultdict(list)\n",
    "        for i, s in enumerate(groupSizes):\n",
    "            cnt[s].append(i)\n",
    "        ret = []\n",
    "        for k, v in cnt.items():\n",
    "            tmp = []\n",
    "            for x in v:\n",
    "                if len(tmp) == k:\n",
    "                    ret.append(tmp)\n",
    "                    tmp = []\n",
    "                tmp.append(x)\n",
    "            ret.append(tmp)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        groups = defaultdict(list)\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            groups[size].append(i)\n",
    "        ans = []\n",
    "        for size, people in groups.items():\n",
    "            ans.extend(people[i: i + size] for i in range(0, len(people), size))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        # 建立哈希表\n",
    "        hm = defaultdict(list)\n",
    "        # 对其分好组\n",
    "        for i, x in enumerate(groupSizes):\n",
    "            hm[x].append(i)\n",
    "        ans = []\n",
    "        for zs, ls in hm.items():\n",
    "            i, l = 0, len(ls)\n",
    "            while i < l:\n",
    "                ans.append(ls[i :i + zs])\n",
    "                i += zs\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        hashmap = defaultdict(list)\n",
    "        res = []\n",
    "        for i, val in enumerate(groupSizes):\n",
    "            hashmap[val].append(i)\n",
    "            # 满员，进入res\n",
    "            if len(hashmap[val]) == val:\n",
    "                res.append(hashmap.pop(val))\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(v):\n",
    "    for a in v:\n",
    "        yield a\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        dic = defaultdict(list)\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            dic[v].append(i)\n",
    "        res = []\n",
    "        temp= []\n",
    "        for k,v in dic.items():\n",
    "            for a in func1(v):\n",
    "                temp.append(a)\n",
    "                if len(temp)==k:\n",
    "                    res.append(temp)\n",
    "                    temp=[]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        m = {}\n",
    "        for i, g in enumerate(groupSizes):\n",
    "            if g not in m.keys():\n",
    "                m[g] = []\n",
    "            m[g].append(i)\n",
    "        ans = []\n",
    "        for gs, vals in m.items():\n",
    "            cnt = 0\n",
    "            row = []\n",
    "            while vals:\n",
    "                row.append(vals.pop())\n",
    "                cnt += 1\n",
    "                if cnt == gs:\n",
    "                    ans.append(row)\n",
    "                    cnt = 0\n",
    "                    row = []\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        mp = defaultdict(list)\n",
    "        res = []\n",
    "        for i, v in enumerate(groupSizes):\n",
    "            mp[v].append(i)\n",
    "        for k, v in mp.items():\n",
    "            for i in range(0,len(v), k):\n",
    "                res.append(v[i:i+k])\n",
    "        return res\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        mp,ans = dict(),[]\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            if not v in mp or len(ans[mp[v]]) == v:\n",
    "                mp[v] = len(ans)\n",
    "                ans.append([i])\n",
    "            else:\n",
    "                ans[mp[v]].append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        count_d = {}\n",
    "        total = []\n",
    "        k = 0\n",
    "        for i in groupSizes:\n",
    "            # res.append(k)\n",
    "\n",
    "            if str(i) in count_d.keys():\n",
    "                count_d[str(i)].append(k)\n",
    "            else:\n",
    "                count_d[str(i)] = [k]\n",
    "            k += 1\n",
    "        # return count_d\n",
    "        for k,v in count_d.items():\n",
    "            res = []\n",
    "            for idx in v:\n",
    "                res.append(idx)\n",
    "                if len(res) == int(k):\n",
    "                    total.append(res)\n",
    "                    res = []\n",
    "                    \n",
    "            \n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(v):\n",
    "    for a in v:\n",
    "        yield a\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        dic = defaultdict(list)\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            dic[v].append(i)\n",
    "        res = []\n",
    "        temp= []\n",
    "        for k,v in dic.items():\n",
    "            for a in func1(v):\n",
    "                temp.append(a)\n",
    "                if len(temp)==k:\n",
    "                    res.append(temp)\n",
    "                    temp=[]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(v):\n",
    "    for a in v:\n",
    "        yield a\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        dic = defaultdict(list)\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            dic[v].append(i)\n",
    "        res = []\n",
    "        temp= []\n",
    "        for k,v in dic.items():\n",
    "            for a in func1(v):\n",
    "                temp.append(a)\n",
    "                if len(temp)==k:\n",
    "                    res.append(temp)\n",
    "                    temp=[]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        n = len(groupSizes)\n",
    "        f = defaultdict(list) \n",
    "        for i, s in enumerate(groupSizes):\n",
    "            f[s].append(i) \n",
    "        ans = []\n",
    "        for s,vec in f.items():\n",
    "            for i in range(0, len(vec), s):\n",
    "                tmp = []\n",
    "                for j in range(i,i+s):\n",
    "                    tmp.append(vec[j])\n",
    "                ans.append(tmp) \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        groups = {}\n",
    "        result = []\n",
    "        for i in range(len(groupSizes)):\n",
    "            size = groupSizes[i]\n",
    "            if size in groups:\n",
    "                group = groups[size]\n",
    "                group.append(i)\n",
    "            else:\n",
    "                group = [i]\n",
    "                groups[size]=group\n",
    "            if len(group)==size:\n",
    "                groups[size] = []\n",
    "                result.append(group)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        d = defaultdict(list)\n",
    "        for i, x in enumerate(groupSizes):\n",
    "            d[x].append(i)\n",
    "        ans = []\n",
    "        for key, val in d.items():\n",
    "            for i in range(0, len(val), key):\n",
    "                ans.append(val[i: i + key])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        e_dict = collections.defaultdict(list)\n",
    "        ans = []\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            e_dict[size].append(i)\n",
    "        for size in e_dict:\n",
    "            v = e_dict[size]\n",
    "            temp = []\n",
    "            for i, j in enumerate(v):\n",
    "                temp.append(j)\n",
    "                if i % size == size - 1:\n",
    "                    ans.append(temp.copy())\n",
    "                    temp.clear()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        mp, ans = defaultdict(list), []\n",
    "        for i, v in enumerate(groupSizes):\n",
    "            mp[v].append(i)\n",
    "        for k, lt in mp.items():\n",
    "            ans.extend(lt[i: i + k] for i in range(0, len(lt), k))\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        group = collections.defaultdict(list)\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            group[size].append(i)\n",
    "        ans = []\n",
    "        for size, people in group.items():\n",
    "            ans.extend(people[i: i + size] for i in range(0, len(people), size))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(v):\n",
    "    for a in v:\n",
    "        yield a\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        dic = defaultdict(list)\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            dic[v].append(i)\n",
    "        res = []\n",
    "        temp= []\n",
    "        for k,v in dic.items():\n",
    "            for a in func1(v):\n",
    "                temp.append(a)\n",
    "                if len(temp)==k:\n",
    "                    res.append(temp)\n",
    "                    temp=[]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        ans, mp = [], dict()\n",
    "        for i, v in enumerate(groupSizes):\n",
    "            if not v in mp or len(ans[mp[v]]) == v:\n",
    "                mp[v] = len(ans)\n",
    "                ans.append([i])\n",
    "            else:\n",
    "                ans[mp[v]].append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        n=len(groupSizes)\n",
    "        min0=min(groupSizes)\n",
    "        max0=max(groupSizes)\n",
    "        ans=[]\n",
    "        for i in range(min0,max0+1):\n",
    "            storelst=[]\n",
    "            c=0\n",
    "            for j in range(n):\n",
    "                if groupSizes[j]==i:\n",
    "                    c+=1\n",
    "                    storelst.append(j)\n",
    "                if c==i:\n",
    "                    ans.append(storelst)\n",
    "                    storelst=[]\n",
    "                    c=0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        # 创建一个字典，用于存储每个组的成员\n",
    "        groups = {}\n",
    "        result = []\n",
    "        \n",
    "        # 遍历每个人的分组大小和ID\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            # 如果该分组大小不存在于字典中，创建一个空列表\n",
    "            if size not in groups:\n",
    "                groups[size] = []\n",
    "            \n",
    "            # 将当前人的ID添加到对应大小的分组中\n",
    "            groups[size].append(i)\n",
    "            \n",
    "            # 如果分组大小达到要求，将该分组加入结果列表，并清空分组\n",
    "            if len(groups[size]) == size:\n",
    "                result.append(groups[size])\n",
    "                groups[size] = []\n",
    "        \n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        dicts = {}\n",
    "        reslut =[]\n",
    "        for i,g in enumerate(groupSizes):\n",
    "            if g ==1:\n",
    "                reslut.append([i])\n",
    "            elif g not in dicts:\n",
    "                dicts[g]=[i]\n",
    "            else:\n",
    "                dicts[g].append(i)\n",
    "                if len(dicts[g])==g:\n",
    "                    reslut.append(dicts[g])\n",
    "                    dicts[g]=[]\n",
    "        return reslut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        d = {}\n",
    "        GroupResult = []\n",
    "        position = 0\n",
    "        for p in groupSizes:\n",
    "            if p not in d:\n",
    "                d[p] = [position]\n",
    "\n",
    "                position += 1\n",
    "            else:\n",
    "                d[p].append(position)\n",
    "                position += 1\n",
    "        for k in d:\n",
    "            if k == len(d[k]):\n",
    "                GroupResult.append(d[k])\n",
    "            else:\n",
    "                while d[k]:\n",
    "                    SmallGroup = []\n",
    "                    for i in range(k):\n",
    "                        SmallGroup.append(d[k].pop())\n",
    "                    GroupResult.append(SmallGroup)\n",
    "        return GroupResult"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        gSize = []\n",
    "        ans = []\n",
    "        for i in groupSizes:\n",
    "            gSize.append(i)\n",
    "        gSize.sort()\n",
    "        gSize_set = set(gSize)\n",
    "        gSize_list = []\n",
    "        for i in gSize_set:\n",
    "            count_i = gSize.count(i)\n",
    "            for j in range(count_i//i):\n",
    "                gSize_list.append(i)\n",
    "        for j in gSize_list:\n",
    "            tmp = []\n",
    "            while j:\n",
    "                inx = groupSizes.index(j)\n",
    "                tmp.append(inx)\n",
    "                groupSizes[inx] = -1\n",
    "                if len(tmp) == j:\n",
    "                    break\n",
    "\n",
    "            ans.append(tmp)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(v):\n",
    "    for a in v:\n",
    "        yield a\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        dic = defaultdict(list)\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            dic[v].append(i)\n",
    "        res = []\n",
    "        temp= []\n",
    "        for k,v in dic.items():\n",
    "            for a in func1(v):\n",
    "                temp.append(a)\n",
    "                if len(temp)==k:\n",
    "                    res.append(temp)\n",
    "                    temp=[]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        groups=defaultdict(list)\n",
    "        for people,size in enumerate(groupSizes):\n",
    "            groups[size].append(people)\n",
    "        result=[]\n",
    "        for size,people in groups.items():\n",
    "            result.extend(people[i:i+size]  for i in range(0,len(people),size))\n",
    "        return result        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        mp = defaultdict(list)\n",
    "        res = []\n",
    "        for i, v in enumerate(groupSizes):\n",
    "            mp[v].append(i)\n",
    "        for k, lt in mp.items():\n",
    "            res.extend(lt[i:i+k] for i in range(0, len(lt), k))\n",
    "        return res\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        #copy\n",
    "        groups = defaultdict(list)\n",
    "        for i,size in enumerate(groupSizes):\n",
    "            groups[size].append(i)\n",
    "        ans = []\n",
    "        for size, people in groups.items():\n",
    "            ans.extend(people[i:i+size] for i in range(0,len(people),size))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(v):\n",
    "    for a in v:\n",
    "        yield a\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        dic = defaultdict(list)\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            dic[v].append(i)\n",
    "        res = []\n",
    "        temp= []\n",
    "        for k,v in dic.items():\n",
    "            for a in func1(v):\n",
    "                temp.append(a)\n",
    "                if len(temp)==k:\n",
    "                    res.append(temp)\n",
    "                    temp=[]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        group_dict = {}\n",
    "        result = []\n",
    "        for i in range(len(groupSizes)):\n",
    "            if groupSizes[i] not in group_dict:\n",
    "                group_dict[groupSizes[i]] = [1, [i]]\n",
    "            else:\n",
    "                if groupSizes[i] == group_dict[groupSizes[i]][0]:\n",
    "                    result.append(group_dict[groupSizes[i]][1])\n",
    "                    del group_dict[groupSizes[i]]\n",
    "                if groupSizes[i] not in group_dict:\n",
    "                        group_dict[groupSizes[i]] = [1, [i]]\n",
    "                else:\n",
    "                    group_dict[groupSizes[i]][0] += 1\n",
    "                    group_dict[groupSizes[i]][1].append(i)\n",
    "        print(group_dict)  \n",
    "        for key in group_dict:\n",
    "            if key == group_dict[key][0]:\n",
    "                result.append(group_dict[key][1])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(v):\n",
    "    for a in v:\n",
    "        yield a\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        dic = defaultdict(list)\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            dic[v].append(i)\n",
    "        res = []\n",
    "        temp= []\n",
    "        for k,v in dic.items():\n",
    "            for a in func1(v):\n",
    "                temp.append(a)\n",
    "                if len(temp)==k:\n",
    "                    res.append(temp)\n",
    "                    temp=[]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        groups = defaultdict(list)\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            groups[size].append(i)\n",
    "        ans = []\n",
    "        for size, people in groups.items():\n",
    "            ans.extend(people[i: i + size] for i in range(0, len(people), size))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "\n",
    "        dic = {}\n",
    "\n",
    "        l = len(groupSizes)\n",
    "\n",
    "        for i in range(l):\n",
    "            if groupSizes[i] in dic:\n",
    "                dic[groupSizes[i]].append(i)\n",
    "            else:\n",
    "                dic[groupSizes[i]] = [i]\n",
    "\n",
    "        res = []\n",
    "        for i in dic:\n",
    "            l = len(dic[i])\n",
    "            j = 0\n",
    "            s = []\n",
    "            while j < l:\n",
    "                s.append(dic[i][j])\n",
    "                if len(s) == i:\n",
    "                    res.append(s)\n",
    "                    s = []\n",
    "                j += 1\n",
    "        return (res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        res = []\n",
    "        self.map = {}\n",
    "        for key, value in enumerate(groupSizes):\n",
    "            if value not in self.map:\n",
    "                self.map[value] = []\n",
    "            \n",
    "            if len(self.map[value]) == value:\n",
    "                res.append(self.map[value])\n",
    "                self.map[value] = []\n",
    "\n",
    "            self.map[value].append(key)\n",
    "        \n",
    "        for value in self.map.values():\n",
    "            res.append(value)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "\n",
    "\n",
    "        check = collections.defaultdict(list)\n",
    "        for i,x in enumerate(groupSizes):\n",
    "            check[x].append(i)\n",
    "        #print(check)\n",
    "        ans = []\n",
    "        for x in check:\n",
    "            nums = check[x]\n",
    "            for i in range(0,len(nums),x):\n",
    "                ans.append(nums[i:i+x])\n",
    "        return ans \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        memo = collections.defaultdict(list)\n",
    "        for i, sz in enumerate(groupSizes):\n",
    "            memo[sz].append(i)\n",
    "            if len(memo[sz]) == sz:\n",
    "                ans.append(memo[sz])\n",
    "                del memo[sz]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, group_sizes: List[int]) -> List[List[int]]:\n",
    "        cur_group_idx = {}\n",
    "        ans = []\n",
    "        for i, size in enumerate(group_sizes):\n",
    "            if size not in cur_group_idx or len(ans[cur_group_idx[size]]) == size:\n",
    "                ans.append([i])\n",
    "                cur_group_idx[size] = len(ans) - 1\n",
    "            else:\n",
    "                ans[cur_group_idx[size]].append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        group = defaultdict(list)\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            group[size].append(i)\n",
    "        ans = []\n",
    "        for size, people in group.items():\n",
    "            #拼接\n",
    "            ans.extend(people[i:i + size] for i in range(0, len(people), size))\n",
    "        return ans \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(v):\n",
    "    for a in v:\n",
    "        yield a\n",
    "\n",
    "class Solution:\n",
    "\n",
    "\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        dic = defaultdict(list)\n",
    "        for i,v in enumerate(groupSizes):\n",
    "            dic[v].append(i)\n",
    "        print(dic)\n",
    "        res = []\n",
    "        temp= []\n",
    "        for k,v in dic.items():\n",
    "            for a in func1(v):\n",
    "                temp.append(a)\n",
    "                if len(temp)==k:\n",
    "                    res.append(temp)\n",
    "                    temp=[]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        resultList=checkList=tempgroup=[]\n",
    "        tempgroupSize=k=0\n",
    "        flag=1\n",
    "        for i in range(0,len(groupSizes)):\n",
    "            checkList=checkList+[0]\n",
    "        while(flag==1):\n",
    "            for j in range(0,len(groupSizes)):\n",
    "                if checkList[j]==1: continue\n",
    "                elif checkList[j]==0 and tempgroupSize==0:\n",
    "                    tempgroupSize=groupSizes[j]\n",
    "                elif checkList[j]==0 and groupSizes[j]==tempgroupSize and k<tempgroupSize:\n",
    "                    tempgroup=tempgroup+[j]\n",
    "                    checkList[j]=1\n",
    "                    k+=1\n",
    "                    if k==tempgroupSize:\n",
    "                        resultList.append(tempgroup)\n",
    "                        tempgroupSize=k=j=0\n",
    "                        tempgroup=[]\n",
    "                        break\n",
    "            flag=0\n",
    "            for i in checkList:\n",
    "                if i==0:\n",
    "                    flag=1\n",
    "                    break\n",
    "        return resultList\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        d = dict()\n",
    "        for i in range(len(groupSizes)):\n",
    "            if groupSizes[i] not in d:\n",
    "                d[groupSizes[i]] = []\n",
    "                d[groupSizes[i]].append([i])\n",
    "            else:\n",
    "                if len(d[groupSizes[i]][-1]) == groupSizes[i]:\n",
    "                    d[groupSizes[i]].append([])\n",
    "                d[groupSizes[i]][-1].append(i)\n",
    "        for k in d:\n",
    "            for j in d[k]:\n",
    "                ans.append(j)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        grouplist ={}\n",
    "        grouplen = len(groupSizes)\n",
    "        groupnum = 0\n",
    "        result = []\n",
    "        for i in range(grouplen):\n",
    "            grouplist[groupSizes[i]] = []\n",
    "        for i in range(grouplen):\n",
    "            grouplist[groupSizes[i]].append(i)\n",
    "        print(grouplist)\n",
    "        for keys,values in grouplist.items():\n",
    "            if(keys < len(values)):\n",
    "                i = 0\n",
    "                while i <len(values):\n",
    "                    result.append(grouplist[keys][i:i + keys])\n",
    "                    i += keys\n",
    "            else:\n",
    "                result.append(values)\n",
    "        #print(result)\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        d = defaultdict(list)\n",
    "        for i,x in enumerate(groupSizes):\n",
    "            d[x].append(i)\n",
    "        ans = []\n",
    "        for k,v in d.items():\n",
    "            for i in range(0,len(v),k):\n",
    "                ans.append(v[i:i+k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        res = [[]]\n",
    "        idx = list(range(len(groupSizes)))\n",
    "        idx.sort(key=lambda x: -groupSizes[x])\n",
    "        for i in idx:\n",
    "            if groupSizes[i] > len(res[-1]):\n",
    "                res[-1].append(i)\n",
    "            else:\n",
    "                res.append([i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        _map = {}\n",
    "        ans = []\n",
    "        for i in range(len(groupSizes)):\n",
    "            if groupSizes[i] in _map:\n",
    "                _map[groupSizes[i]].append(i)    \n",
    "            else:\n",
    "                _map[groupSizes[i]] = [i]\n",
    "            if len(_map[groupSizes[i]]) == groupSizes[i]:\n",
    "                ans.append(_map[groupSizes[i]])\n",
    "                _map[groupSizes[i]] = []\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        List_1 = [x for x in range(len(groupSizes))]\n",
    "        List_1.sort(key=lambda z:groupSizes[z])\n",
    "        i = 0\n",
    "        while i < len(List_1):\n",
    "            ans.append(List_1[i:i + groupSizes[List_1[i]]])\n",
    "            i += groupSizes[List_1[i]]\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        d = {}\n",
    "        l = len(groupSizes)\n",
    "        for i in range(l):\n",
    "            if groupSizes[i] not in d.keys():\n",
    "                d[groupSizes[i]] = [[i]]\n",
    "            else:\n",
    "                if len(d[groupSizes[i]][-1]) != groupSizes[i]:\n",
    "                    d[groupSizes[i]][-1].append(i)\n",
    "                else:\n",
    "                    d[groupSizes[i]].append([i])\n",
    "        ans = []\n",
    "        for i in d:\n",
    "            ans.extend(d[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        a = {}\n",
    "        n = len(groupSizes)\n",
    "        for i in range(n):\n",
    "            k = groupSizes[i]\n",
    "            if k in a:\n",
    "                a[k].append(i)\n",
    "            else:\n",
    "                a[k] = [i]\n",
    "        ans = []\n",
    "        for j in a.keys():\n",
    "            b = a[j]\n",
    "            m = len(b)\n",
    "            for l in range(m//j):\n",
    "                ans.append(b[l*j:(l+1)*j])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        groupSizes=[[groupSizes[i],i] for i in range(len(groupSizes))]\n",
    "\n",
    "        groupSizes=sorted(groupSizes)\n",
    "\n",
    "        #print(groupSizes)\n",
    "\n",
    "        ans=[]\n",
    "        i=0\n",
    "        while(i<len(groupSizes)):\n",
    "            size,index=groupSizes[i]\n",
    "\n",
    "            ans.append([groupSizes[k][1] for k in range(i,i+size)])\n",
    "            i+=size \n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        mp, ans = defaultdict(list), []\n",
    "        for i, v in enumerate(groupSizes):\n",
    "            mp[v].append(i)\n",
    "        for k, lt in mp.items():\n",
    "            ans.extend(lt[i:i+k] for i in range(0, len(lt), k))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        group_dict = {}\n",
    "        result = []\n",
    "        for i in range(len(groupSizes)):\n",
    "            if groupSizes[i] not in group_dict:\n",
    "                group_dict[groupSizes[i]] = [1, [i]]\n",
    "            else:\n",
    "                if groupSizes[i] == group_dict[groupSizes[i]][0]:\n",
    "                    result.append(group_dict[groupSizes[i]][1])\n",
    "                    del group_dict[groupSizes[i]]\n",
    "                if groupSizes[i] not in group_dict:\n",
    "                        group_dict[groupSizes[i]] = [1, [i]]\n",
    "                else:\n",
    "                    group_dict[groupSizes[i]][0] += 1\n",
    "                    group_dict[groupSizes[i]][1].append(i)\n",
    "        print(group_dict)  \n",
    "        for key in group_dict:\n",
    "            if key == group_dict[key][0]:\n",
    "                result.append(group_dict[key][1])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "      groups = []\n",
    "      for idx, size in enumerate(groupSizes):\n",
    "        groups.append((idx, size))\n",
    "      groups.sort(key=lambda x: x[1])\n",
    "\n",
    "      answer = []\n",
    "      curr_size = 0\n",
    "      for group, size in groups:\n",
    "        if not answer or size == 1:\n",
    "          answer.append([group])\n",
    "        elif len(answer[-1]) < curr_size:\n",
    "          answer[-1].append(group)\n",
    "        else:\n",
    "          answer.append([group])\n",
    "        curr_size = size\n",
    "      return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        size_mapper = collections.defaultdict(list)\n",
    "        ans = []\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            size_mapper[size].append(i)\n",
    "        for size in size_mapper:\n",
    "            v = size_mapper[size]\n",
    "            temp = []\n",
    "            for i, j in enumerate(v):\n",
    "                temp.append(j)\n",
    "                if i % size == size - 1:\n",
    "                    ans.append(temp.copy())\n",
    "                    temp.clear()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        groups = collections.defaultdict(list)\n",
    "        for i, size in enumerate(groupSizes):\n",
    "            groups[size].append(i)\n",
    "        \n",
    "        res = []\n",
    "\n",
    "        for size, people in groups.items():\n",
    "            res.extend(people[i: i + size] for i in range(0, len(people), size))\n",
    "\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # groups = collections.defaultdict(list)\n",
    "        # for i, size in enumerate(groupSizes):\n",
    "        #     groups[size].append(i)\n",
    "        \n",
    "        # res = []\n",
    "        # for size, people in groups.items():\n",
    "        #     res.extend(people[i: i + size] for i in range(0, len(people), size))\n",
    "        \n",
    "        # return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        Dict = {}\n",
    "        res = []\n",
    "        length = len(groupSizes)\n",
    "        for i in range(0,length,1):\n",
    "            if groupSizes[i] in Dict:\n",
    "                Dict[groupSizes[i]].append(i)\n",
    "            else:\n",
    "                Dict.setdefault(groupSizes[i],[i])\n",
    "        for i in Dict:\n",
    "            times = len(Dict[i])//i\n",
    "            index = 0\n",
    "            for j in range(0,times,1):\n",
    "                tempList = []\n",
    "                tempList = Dict[i][index:index+i]\n",
    "                index = index + i\n",
    "                res.append(tempList)\n",
    "        return res\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:\n",
    "        hashmap = {}\n",
    "        res = []\n",
    "        for i, val in enumerate(groupSizes):\n",
    "            if hashmap.get(val) == None: hashmap[val] = []\n",
    "            hashmap[val].append(i)\n",
    "            ans = hashmap[val]\n",
    "            # 满员，进入res\n",
    "            if len(ans) % val == 0:\n",
    "                n = len(ans) // val\n",
    "                res.append(ans[(n-1)*val:n*val])\n",
    "        \n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
